package com.jcw.kqxt.attendance_rule.service;

import com.jcw.kqxt.attendance_rule.mapper.AttendanceRuleDao;
import com.jcw.kqxt.attendance_rule.model.*;
import com.jcw.kqxt.domain.User;
import com.jcw.kqxt.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AttendanceRuleService {

    @Autowired
    private AttendanceRuleDao attendanceRuleDao;

    /**
     * 工作日管理人配置
     */
    @Transactional
    public void addWeekdaysAdmin(ConfigParamDto paramDto) {
        List<Integer> ids = paramDto.getId();
        //清除原有配置
        attendanceRuleDao.reset();
        //添加新配置
        attendanceRuleDao.editConfig(ids);
    }

    /**
     * 人员列表
     */
    public List<User> userList() {
        List<User> list = attendanceRuleDao.userList();
        return list;
    }

    /**
     * 当前考勤内勤
     */
    public List<User> attendanceUser() {
        return attendanceRuleDao.attendanceUser();
    }

    /**
     * 工作日配置列表
     */
    @Transactional
    public RuleResult weekdaysConfig(RuleParamDto paramDto) {
        //1.获取当月天数
        String yearMonth = paramDto.getDate();
        int days = DateUtils.getDaysOfMonth(yearMonth);

        //2.查询数据库，数据库中存在当月，则从数据库中返回，数据库中不存在，则重新构造并存入数据库
        boolean check = checkDate(yearMonth);

        if (check) {
            //3.组装数据结构+1
            RuleResult ruleResult = getConfigFromDB(yearMonth);
            return ruleResult;
        } else {
            //3.组装数据结构
            RuleResult ruleResult = getRuleResult(yearMonth,days);
            //4.保存入库
            insertConfig(ruleResult);
            return ruleResult;
        }
    }

    private RuleResult getConfigFromDB(String yearMonth) {
        RuleResult ruleResult = new RuleResult();
        ruleResult.setDate(yearMonth);
        List<TableData> tableDatas = new ArrayList<>();
        //上午
        TableData am = new TableData();
        am.setName("上午");
        List<Months> amlist = attendanceRuleDao.getAmConfig(yearMonth);
        am.setMonths(amlist);
        tableDatas.add(am);

        //下午
        TableData pm = new TableData();
        pm.setName("下午");
        List<Months> pmlist = attendanceRuleDao.getPmConfig(yearMonth);
        pm.setMonths(pmlist);
        tableDatas.add(pm);

        return ruleResult;
    }

    /**
     *判断数据库中是否存在当月记录
     */
    private boolean checkDate(String yearMonth) {
        int total = attendanceRuleDao.checkDate(yearMonth);
        if (total > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 构造当月考勤日期结构
     * @param yearMonth
     * @param days
     * @return
     */
    private RuleResult getRuleResult(String yearMonth,int days) {
        RuleResult ruleResult = new RuleResult();
        ruleResult.setDate(yearMonth);
        List<TableData> tableDatas = new ArrayList<>();
        //上午
        TableData am = new TableData();
        am.setName("上午");
        List<Months> list = new ArrayList<>();
        for (int i = 0; i < days; i++) {
            Months months = new Months();
            months.setIndex(i);
            months.setValue(1);
            months.setUrl("");
            list.add(months);
        }
        am.setMonths(list);
        tableDatas.add(am);
        //下午
        TableData pm = new TableData();
        pm.setName("下午");
        pm.setMonths(list);
        tableDatas.add(pm);

        ruleResult.setTableData(tableDatas);
        return ruleResult;
    }

    /**
     * 修改工作日
     */
    public RuleResult editWeekdays(RuleParamDto paramDto) {
        //1.清除已有配置
        attendanceRuleDao.resetConfig(paramDto.getDate());

        //2.加入新配置
        RuleResult ruleResult = new RuleResult();
        ruleResult.setDate(paramDto.getDate());
        ruleResult.setTableData(paramDto.getTableData());
        insertConfig(ruleResult);
        return ruleResult;
    }

    private void insertConfig(RuleResult ruleResult) {

        String yearMonth = ruleResult.getDate();
        List<TableData> tableData = ruleResult.getTableData();
        List<Months> amMonths = tableData.get(0).getMonths();
        List<Months> pmMonths = tableData.get(1).getMonths();

        List<RuleInsertVo> list = new ArrayList<>();
        for (int i = 0; i < amMonths.size(); i++) {
            RuleInsertVo vo = new RuleInsertVo();
            vo.setMonthYear(yearMonth);
            vo.setDIndex(i);
            vo.setAmValue(amMonths.get(i).getValue());
            vo.setPmValue(pmMonths.get(i).getValue());
            vo.setAmUrl(amMonths.get(i).getUrl());
            vo.setPmUrl(pmMonths.get(i).getUrl());
            list.add(vo);
        }
        attendanceRuleDao.insertConfig(list);
    }

}
